package com.hy.dao;

import com.hy.bean.Follow;
import com.hy.utils.DBManager;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class FollowDao {
    private QueryRunner qr = new QueryRunner();

    public boolean selectExists(Integer userId,Integer followId) throws Exception {
        Connection conn = DBManager.getConn();
        String sql = "select user_id userId," +
                "user1_id user1Id," +
                "create_time createTime," +
                "update_time updateTime," +
                "create_id createId," +
                "update_id updateId," +
                "tombstone" +
                " from follow where user_id = ? and user1_id = ?";
        Follow follow = qr.query(conn, sql, new BeanHandler<Follow>(Follow.class), userId, followId);
        DbUtils.close(conn);
        if(follow == null)
            return false;
        else
            return true;
    }
    public boolean followUser( boolean exists,Integer userId,Integer followId) throws Exception {
        Connection conn = DBManager.getConn();
        String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        int count = -1;
        if(exists){
            String sql = "update follow set tombstone = '0',update_time=?,update_id=? " +
                    " where (user_id = ? and user1_id = ?) and create_id = ?";
            count = qr.update(conn, sql,time, userId,userId, followId, userId);
        }else{
            String sql = "insert into follow value (?, ?, ?, null, ?, null, '0')";
            count = qr.update(conn, sql, userId, followId, time, userId);
        }

        DbUtils.close(conn);
        return count>0?true:false;
    }

    public boolean unfollowUser(Integer userId,Integer followId) throws Exception {
        Connection conn = DBManager.getConn();
        String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String sql = "update follow set tombstone = '1',update_time=?,update_id=? " +
                " where (user_id = ? and user1_id = ?) and create_id = ?";
        int count = qr.update(conn, sql,time, userId,userId, followId, userId);
        DbUtils.close(conn);
        return count>0?true:false;
    }

    public int selectCount(Integer userId) throws Exception {
        Connection conn = DBManager.getConn();
        String sql = "select count(*) as value  from ums_user u where id not in (\n" +
                "                 select user1_id from follow f where user_id = ? and f.tombstone='0' \n" +
                "                ) and id != ? and u.tombstone='0' ";
        Map<String, Object> map = qr.query(conn, sql, new MapHandler(),userId,userId);
        DbUtils.close(conn);
        return Integer.parseInt(map.get("value").toString());
    }


    public List<Map<String, Object>> selectRecommend(Integer userId, int anInt) throws Exception {
        Connection conn = DBManager.getConn();
        String sql = "select id,user_name,img from ums_user u where id not in ( " +
                " select user1_id from follow f where user_id = ? and f.tombstone='0'" +
                " ) and id != ? and u.tombstone='0' limit ?,5";

        MapListHandler mlh = new MapListHandler();
        List<Map<String, Object>> mapList = qr.query(conn, sql, mlh, userId,userId, anInt);
        DbUtils.close(conn);
        return mapList;
    }

    public static void main(String[] args) throws Exception {
//        followUser
        FollowDao followDao = new FollowDao();
        /*boolean b = followDao.followUser(false, 10, 3);
        System.out.println(b);*/
    }
}
